package com.example.cloud.service.finance.impl;

import com.example.cloud.constants.StateConstants;
import com.example.cloud.dao.business.medical.TreatPlanCourseDetailsRepository;
import com.example.cloud.dao.business.medical.TreatPlanCourseProductRepository;
import com.example.cloud.dao.business.medical.TreatPlanHomeProductRepository;
import com.example.cloud.dao.business.medical.TreatPlanHomeProjectRepository;
import com.example.cloud.dao.finance.DeductRecordRepository;
import com.example.cloud.model.business.medical.TreatPlanCourseDetails;
import com.example.cloud.model.business.medical.TreatPlanCourseProduct;
import com.example.cloud.model.business.medical.TreatPlanHomeProduct;
import com.example.cloud.model.business.medical.TreatPlanHomeProject;
import com.example.cloud.model.finance.DeductRecord;
import com.example.cloud.service.finance.DeductRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangtao on 2018/5/22.
 */
@Service
public class DeductRecordServiceImpl implements DeductRecordService {

    @PersistenceContext
    EntityManager em;

    @Autowired
    private DeductRecordRepository deductRecordRepository;

    @Autowired
    private TreatPlanCourseDetailsRepository treatPlanCourseDetailsRepository;

    @Autowired
    private TreatPlanCourseProductRepository treatPlanCourseProductRepository;

    @Autowired
    private TreatPlanHomeProductRepository treatPlanHomeProductRepository;

    @Autowired
    private TreatPlanHomeProjectRepository treatPlanHomeProjectRepository;

    @Override
    public List<DeductRecord> findByPatientId(Long patientId) {
        List<DeductRecord> resultList = new ArrayList<>();
        List<TreatPlanCourseDetails> list1 = treatPlanCourseDetailsRepository.findByPatientIdAndExecuteStatus(patientId, StateConstants.EXECUTE_STATUS_YES);
        list1.forEach(details -> {
            DeductRecord deductRecord = new DeductRecord();
            deductRecord.setPatientId(details.getPatientId());
            deductRecord.setMemberId(details.getMemberId());
            deductRecord.setTreatId(details.getCourseId());
            deductRecord.setTreatType(StateConstants.TREAT_TYPE_COURSE_DETAILS);
            deductRecord.setTreatName(details.getCourseName());
            deductRecord.setBusinessId(details.getId());
            deductRecord.setItemId(details.getItemId());
            deductRecord.setItemName(details.getItemName());
            deductRecord.setPrice(details.getPrice());
            deductRecord.setTotalDeductCount(details.getQuantity());
            deductRecord.setTickedCount(details.getDeduct());
            deductRecord.setDeductCount(1);
            deductRecord.setDeductAmount(details.getPrice());
            resultList.add(deductRecord);
        });
        List<TreatPlanCourseProduct> list2 = treatPlanCourseProductRepository.findByPatientIdAndExecuteStatus(patientId, StateConstants.EXECUTE_STATUS_YES);
        list2.forEach(product -> {
            DeductRecord deductRecord = new DeductRecord();
            deductRecord.setPatientId(product.getPatientId());
            deductRecord.setMemberId(product.getMemberId());
            deductRecord.setTreatId(product.getId());
            deductRecord.setTreatType(StateConstants.TREAT_TYPE_COURSE_PRODUCT);
            deductRecord.setTreatName(product.getProductName());
            deductRecord.setBusinessId(product.getId());
            deductRecord.setItemId(product.getProductId());
            deductRecord.setItemName(product.getProductName());
            deductRecord.setPrice(product.getPrice());
            deductRecord.setTotalDeductCount(product.getQuantity());
            deductRecord.setTickedCount(product.getDeduct());
            deductRecord.setDeductCount(1);
            deductRecord.setDeductAmount(product.getPrice());
            resultList.add(deductRecord);
        });
        List<TreatPlanHomeProject> list3 = treatPlanHomeProjectRepository.findByPatientIdAndExecuteStatus(patientId, StateConstants.EXECUTE_STATUS_YES);
        list3.forEach(project -> {
            DeductRecord deductRecord = new DeductRecord();
            deductRecord.setPatientId(project.getPatientId());
            deductRecord.setMemberId(project.getMemberId());
            deductRecord.setTreatId(project.getHomeId());
            deductRecord.setTreatType(StateConstants.TREAT_TYPE_HOME_PROJECT);
            deductRecord.setTreatName(project.getHomeName());
            deductRecord.setBusinessId(project.getId());
            deductRecord.setItemId(project.getProjectId());
            deductRecord.setItemName(project.getProjectName());
            deductRecord.setPrice(project.getPrice());
            deductRecord.setTotalDeductCount(project.getQuantity());
            deductRecord.setTickedCount(project.getDeduct());
            deductRecord.setDeductCount(1);
            deductRecord.setDeductAmount(project.getPrice());
            resultList.add(deductRecord);
        });
        List<TreatPlanHomeProduct> list4 = treatPlanHomeProductRepository.findByPatientIdAndExecuteStatus(patientId, StateConstants.EXECUTE_STATUS_YES);
        list4.forEach(product -> {
            DeductRecord deductRecord = new DeductRecord();
            deductRecord.setPatientId(product.getPatientId());
            deductRecord.setMemberId(product.getMemberId());
            deductRecord.setTreatId(product.getHomeId());
            deductRecord.setTreatType(StateConstants.TREAT_TYPE_HOME_PRODUCT);
            deductRecord.setTreatName(product.getHomeName());
            deductRecord.setBusinessId(product.getId());
            deductRecord.setItemId(product.getProductId());
            deductRecord.setItemName(product.getProductName());
            deductRecord.setPrice(product.getPrice());
            deductRecord.setTotalDeductCount(product.getQuantity());
            deductRecord.setTickedCount(product.getDeduct());
            deductRecord.setDeductCount(1);
            deductRecord.setDeductAmount(product.getPrice());
            resultList.add(deductRecord);
        });
        return resultList;
    }
}
